const request = require('./request.js')
const MS = require('./ms')
const Error = require('./error')
const storage = require('./storage')
const {
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isObject
} = require('./dataType')

const sellerAccount = class sellerAccount {
    constructor() {
        this.header = {
            'content-type': 'application/json',
            'X-MS-ApplicationId': MS._config.applicationId,
            'X-MS-REST-API-KEY': MS._config.applicationKey,
            'X-MS-APP-ID': MS._config.appid
        }
    }

    login(username, password) {
        let params = {
            username: username,
            password: password
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SELLER_ACCOUNT.LOGIN.url,
                MS._config.parameters.SELLER_ACCOUNT.LOGIN.method, params).then(res => {
                if(res.success) {
                    storage.save("sellerAccount", res.data)
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SELLER_ACCOUNT.GET_SELLER_ACCOUNT.url,
                MS._config.parameters.SELLER_ACCOUNT.GET_SELLER_ACCOUNT.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    create(options) {
        let params = {}
        if(!isUndefined(options.username) && isString(options.username)) {
            params.username = options.username;
        }
        if(!isUndefined(options.password) && isString(options.password)) {
            params.password = options.password;
        }
        if(!isUndefined(options.tempPassword) && isString(options.tempPassword)) {
            params.tempPassword = options.tempPassword;
        }
        if(!isUndefined(options.isSuper) && isBoolean(options.isSuper)) {
            params.isSuper = options.isSuper;
        }
        if(!isUndefined(options.isAdmin) && isBoolean(options.isAdmin)) {
            params.isAdmin = options.isAdmin;
        }
        if(!isUndefined(options.sellerId) && isString(options.sellerId)) {
            params.sellerId = options.sellerId;
        }
        if(!isUndefined(options.sellerName) && isString(options.sellerName)) {
            params.sellerName = options.sellerName;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SELLER_ACCOUNT.CREATE.url,
                MS._config.parameters.SELLER_ACCOUNT.CREATE.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    perfect(options) {
        let params = {
            id: this.current().id
        }
        if(!isUndefined(options.email) && isString(options.email)) {
            params.email = options.email;
        }
        if(!isUndefined(options.mobile) && isString(options.mobile)) {
            params.mobile = options.mobile;
        }
        if(!isUndefined(options.realName) && isString(options.realName)) {
            params.realName = options.realName;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SELLER_ACCOUNT.PERFECT.url,
                MS._config.parameters.SELLER_ACCOUNT.PERFECT.method, params).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    updatePassword(options) {
        let params = {
            id: this.current().id
        }
        if(!isUndefined(options.oldPassword) && isString(options.oldPassword)) {
            params.oldPassword = options.oldPassword;
        }
        if(!isUndefined(options.newPassword) && isString(options.newPassword)) {
            params.newPassword = options.newPassword;
        }
        if(!isUndefined(options.tempPassword) && isString(options.tempPassword)) {
            params.tempPassword = options.tempPassword;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SELLER_ACCOUNT.UPDATE_PASSWORD.url,
                MS._config.parameters.SELLER_ACCOUNT.UPDATE_PASSWORD.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    current() {
        const data = storage.fetch('sellerAccount');
        if(!isUndefined(data)) {
            return typeof data === 'object' ? data : JSON.parse(data)
        } else {
            return null;
        }
    }

    logout() {
        storage.remove("sellerAccount")
    }

    query(options) {
        let params = {
            pageSzie: 10,
            pageNum: 1
        }
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.sellerId) && isString(options.sellerId)) {
                params.sellerId = options.sellerId;
            }
            if(!isUndefined(options.searchName) && isString(options.searchName)) {
                params.searchName = options.searchName;
            }
            if(!isUndefined(options.isSuper) && isBoolean(options.isSuper)) {
                params.isSuper = options.isSuper;
            }
            if(!isUndefined(options.isAdmin) && isBoolean(options.isAdmin)) {
                params.isAdmin = options.isAdmin;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.PageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(
                MS._config.parameters.SELLER_ACCOUNT.QUERY.url,
                MS._config.parameters.SELLER_ACCOUNT.QUERY.method,
                params
            ).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    updateStatus(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SELLER_ACCOUNT.UPDATE_STATUS.url,
                MS._config.parameters.SELLER_ACCOUNT.UPDATE_STATUS.method, params).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}
module.exports = sellerAccount